﻿
using Sys;

using System.Silverlight;
using System.Silverlight.Presentation;
using System.Silverlight.Presentation.Animation;
using System;
using seh = Sys.EventHandler;
using sea = Sys.EventArgs;

namespace SBT
{
    public class HorizontalPageFlipScroller : PageFlipScrollerBase
    {
        protected override string TranslateAxis
        {
            get { return "X"; }
        }
        private Button m_leftButton;
        public Button LeftButton
        {
            get
            {
                return m_leftButton;
            }
            set
            {
                if (m_leftButton != value)
                {
                    m_leftButton = value;
                    m_leftButton.add_click(m_onLeftClick);
                }
            }
        }
        private Button m_rightButton;
        public Button RightButton
        {
            get
            {
                return m_rightButton;
            }
            set
            {
                if (m_rightButton != value)
                {
                    m_rightButton = value;
                    m_rightButton.add_click(m_onRightClick);
                }
            }
        }
       
        seh m_onLeftClick;
        seh m_onRightClick;

        private void OnLeftClick(object sender, sea args)
        {
            if ((bool)(object)ScrollContainer)
            {
                double current = AnimRunning? -DoubleKey.Value :  ScrollContainer.ScrollX;
                double step = ((bool)(object)StepSize) ? StepSize : ScrollContainer.Width;
                double realMax = ScrollContainer.MaxScrollX;
                double maxPage = Math.Ceil(realMax / step);
                realMax = maxPage * step;
                double currentPage = Math.Ceil(current / step);
                double to = 0;
                if (currentPage <= 0)
                    to = 0;
                else if (currentPage >= maxPage)
                    to = realMax - step;
                else
                {
                    to = step * (currentPage - 1);
                }
                if (to == current)
                    return;
                //m_scrollContainer.ScrollX = to;
                InitAnim();
                
                DoubleKey.Value = -to;
                BeginAnimation();
            }
        }
        private void OnRightClick(object sender, sea args)
        {
            if ((bool)(object)ScrollContainer)
            {
                double current = AnimRunning ? -DoubleKey.Value : ScrollContainer.ScrollX;
                double step = ((bool)(object)StepSize) ? StepSize : ScrollContainer.Width;
                double realMax = ScrollContainer.MaxScrollX;
                double maxPage = Math.Ceil(realMax / step);
                realMax = maxPage * step;
                double currentPage = Math.Ceil(current / step);
                double to = 0;
                if (currentPage <= 0)
                    to = step;
                else if (currentPage >= maxPage)
                    to = realMax;
                else
                {
                    to = step * (currentPage + 1);
                }
                if (to == current)
                    return;
                //m_scrollContainer.ScrollX = to;
                InitAnim();
                
                DoubleKey.Value = -to;
                BeginAnimation();
            }
        }

       
        public HorizontalPageFlipScroller()
        {

            m_onLeftClick = OnLeftClick;
            m_onRightClick = OnRightClick;
        }
        
    }
}